package servlet;

import pojo.Members;
import dao.AdminSql;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/AdminServlet")
@MultipartConfig
public class AdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m=request.getParameter("m");
        if("queryMember".equals(m)){
            queryMember(request,response);
        }
        //添加管理员
        else if("addMember".equals(m)){
            addMember(request,response);
        }
        //查询管理员
        else if("queryAdminName".equals(m)){
            queryAdminName(request,response);
        }
        //传递信息到修改密码页面
        else if("updatePwd1".equals(m)){
            updatePwd1(request,response);
        }
        //在修改页面修改密码
        else if("updatePwd2".equals(m)){
            updatePwd2(request,response);
        }
        //修改管理员信息
        else if("updateAdmin".equals(m)){
            updateAdmin(request,response);
        }
        //查看登录的管理员资料
        else if ("queryAdminData".equals(m)){
            try {
                queryAdminData(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    protected void queryAdminData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String name=request.getParameter("adminName");
        Members members = AdminSql.queryAdmin(name);
        String sex=members.getSex();
        String tel=members.getTel();
        String headPortrait=members.getHeadPortrait();
        request.setAttribute("name",name);
        request.setAttribute("sex",sex);
        request.setAttribute("tel",tel);
        System.out.println(tel+123);
        request.setAttribute("headPortrait",headPortrait);
        request.getRequestDispatcher("queryAdminData.jsp").forward(request,response);
    }

    protected void updatePwd2(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException    {
        String oldpwd = AdminSql.getOldpwd(Integer.parseInt(request.getParameter("id")));
        System.out.println(oldpwd);
        String o_pwd = request.getParameter("o_pwd");
        String n_pwd = request.getParameter("n_pwd");
        String pwd=request.getParameter("pwd");
        if(oldpwd.equals(o_pwd)){
            if(n_pwd.equals(pwd)){
                AdminSql.updatePwd(Integer.parseInt(request.getParameter("id")), n_pwd);
                request.setAttribute("msg", "密码修改成功!");
                request.getRequestDispatcher("adminLoginResult.jsp").forward(request, response);
            }else{
                int id=Integer.parseInt(request.getParameter("id"));
                request.setAttribute("id", id);
                request.setAttribute("msg", "两次输入的新密码不一致！");
                request.getRequestDispatcher("updateAdminpwd.jsp").forward(request, response);
            }
        }
        else{
            int id=Integer.parseInt(request.getParameter("id"));
            request.setAttribute("id", id);
            request.setAttribute("msg", "原密码错误！");
            request.getRequestDispatcher("updateAdminpwd.jsp").forward(request, response);
        }
    }

    protected void updateAdmin(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException   {
        Members m=new Members();
        m.setId(Integer.parseInt(request.getParameter("m_id")));
        System.out.println(Integer.parseInt(request.getParameter("m_id")));
        m.setName(request.getParameter("m_name"));
        m.setSex(request.getParameter("m_sex"));
        m.setTel(request.getParameter("m_tel"));
        String m_name=request.getParameter("m_name");
        if(AdminSql.updateMember(m)==true){
            request.setAttribute("msg", "修改成功");
            request.getRequestDispatcher("adminLoginResult.jsp").forward(request, response);
        }else {
            String msg="修改信息错误";
            response.sendRedirect("AdminServlet?m=queryAdminName&&name"+ URLEncoder.encode(m_name,"UTF-8")+"&&msg="+URLEncoder.encode(msg,"UTF-8"));
        }

    }

    protected void updatePwd1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        request.setAttribute("id", request.getParameter("id"));
        request.getRequestDispatcher("updateAdminpwd.jsp").forward(request, response);
    }

    protected void queryAdminName(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException  {
        String name=request.getParameter("name");
        List<Members> list = AdminSql.queryMember(name);
        Members m=new Members();
        for(int i=0;i<list.size();i++){
            m=(Members)list.get(i);
        }
        request.setAttribute("id", m.getId());
        request.setAttribute("name", m.getName());
        request.setAttribute("sex", m.getSex());
        request.setAttribute("tel", m.getTel());
        request.getRequestDispatcher("queryMemberMessage.jsp").forward(request, response);
    }

    protected void addMember(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        try {
            Part part=request.getPart("photo");
            String filename=part.getSubmittedFileName();
            if(filename.equals("")){
                filename="images/default.jpg";
            }else {
                filename="images/"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
                part.write(this.getServletContext().getRealPath("/")+filename);
            }
            Members m=new Members();
            m.setName(request.getParameter("u_name"));
            m.setSex(request.getParameter("u_sex"));
            m.setPwd(request.getParameter("u_pwd"));
            m.setTel(request.getParameter("u_tel"));
            System.out.println(m);
            m.setHeadPortrait(filename);
            System.out.println(filename);
            Boolean aBoolean = AdminSql.addMembers(m);
            if(request.getParameter("u_name")!=null&&request.getParameter("u_sex")!=null&&request.getParameter("u_pwd")!=null&&request.getParameter("u_tel")!=null&&aBoolean==true){
                request.setAttribute("msg", "添加成功");
                request.getRequestDispatcher("adminLoginResult.jsp").forward(request, response);
            }
        }catch (Exception e){
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("addMember.jsp").forward(request, response);
            System.out.println(012);
        }

    }

    protected void queryMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String u_name=request.getParameter("u_name");
        String u_sex=request.getParameter("u_sex");
        List<Members> list = AdminSql.getMemberName(u_name, u_sex);
        System.out.println(list);
        if(list.size()==0){
            request.setAttribute("msg", "没有此管理员");
            request.getRequestDispatcher("adminLoginResult.jsp").forward(request,response);
        }else {
            request.setAttribute("list", list);
            request.getRequestDispatcher("queryMember.jsp").forward(request, response);
        }
    }
}
